#------------------------------------------------------------------------------
# Name:        xml2ini.py
# Purpose:     Eclipse XML color theme converter to PyScripter INI theme
#
# Author:      debjan
#
# Created:     08.12.2012
# Copyright:   (c) debjan 2012
# Licence:     BSD
#------------------------------------------------------------------------------


def main(f):
    import xml.etree.ElementTree as ET
    m = {
        'foreground': ['NonKey', 'Banner', 'Text', 'Identifier'],
        'background': ['Space'],
        'selxml_eionForeground': [],
        'selxml_eionBackground': [],
        'currentLine': [],
        'lineNumber': [],
        'searchResultIndication': [],
        'filteredSearchResultIndication': [],
        'occurrenceIndication': [],
        'writeOccurrenceIndication': [],
        'findScope': [],
        'sourceHoverBackground': [],
        'singleLineComment': ['Comment'],
        'multiLineComment': [],
        'commentTaskTag': [],
        'javadoc': ['DocString', 'MultiLineString'],
        'javadocLink': [],
        'javadocTag': [],
        'javadocKeyword': ['Sxml_eion'],
        'class': ['ClassName', 'MatchingBrace'],
        'interface': [],
        'method': ['FunctionName', 'Direc'],
        'methodDeclaration': [],
        'bracket': [],
        'number': ['Number', 'Hex', 'Octal', 'Float'],
        'string': ['String', 'Output', 'Prompt', 'Char'],
        'operator': ['Symbol'],
        'keyword': ['Key'],
        'annotation': ['CodeComment', 'ASM'],
        'staticMethod': [],
        'localVariable': [],
        'localVariableDeclaration': [],
        'field': ['System'],
        'staticField': [],
        'staticFinalField': [],
        'deprecatedMember': ['Error', 'UnbalancedBrace', 'Traceback', 'Invalid'],
        'deletionIndication': [],
        'enum': [],
        'inheritedMethod': [],
        'abstractMethod': [],
        'parameterVariable': [],
        'typeArgument': [],
        'typeParameter': [],
        'constant': []
    }
    xml_e = ET.parse(f).getroot()
    try:
        xml_p = ET.parse('pyscripter.xml').getroot()
    except IOError:
        raise RuntimeError('pyscripter.xml file is missing.')
    langs = ['Python', 'Cython', 'Intepreter', 'INI', 'C/C++']
    ini = open(xml_e.attrib['name'] + '.ini', 'w')
    for lang in xml_p:
        if lang.attrib['name'] in langs:
            attrs = [x.text for x in lang]
            for style in xml_e:
                h = xml_e.find('background').attrib['color'][1:]
                bg = int('%s%s%s' % (h[4:6], h[2:4], h[0:2]), 16)
                if m.get(style.tag):
                    k = xml_e.find(style.tag)
                    for l in m[style.tag]:
                        if l in attrs:
                            h = k.attrib['color'][1:]
                            fg = int('%s%s%s' % (h[4:6], h[2:4], h[0:2]), 16)
                            ini.write('[Highlighters\%s\%sAttri]\n' % (lang.attrib['name'], l))
                            ini.write('Background=%s\n' % bg)
                            ini.write('Foreground=%s\n\n' % fg)
                            for a in ['bold', 'italic', 'underline', 'strikethrough']:
                                if not k.attrib.get(a):
                                    k.set(a, 'false')
                            ini.write('[Highlighters\%s\%sAttri\Style]\n' % (lang.attrib['name'], l))
                            ini.write('fsBold=%s\n' % k.attrib.get('bold'))
                            ini.write('fsItalic=%s\n' % k.attrib.get('italic'))
                            ini.write('fsUnderline=%s\n' % k.attrib.get('underline'))
                            ini.write('fsStrikeOut=%s\n\n' % k.attrib.get('strikethrough'))
    ini.close()

if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:
        main(sys.argv[1])
    else:
        print('Usage: python xml2ini.py eclipse-theme.xml')
